#coding:utf-8
from django.template import RequestContext
from django.http import HttpResponse,HttpResponseRedirect
from django.shortcuts import render
import json

import common.httpclient as http
import common.tips as tips
import common.modelAdminUser as modelAdminUser
import config.myurls as myurls
import common.function as myfc
import common.category as mycategory


def list(req):
  '管理员帐号'
  
  main_nav=[]
  main_nav.append({"url":"","name":"管理员帐号"})
  context={}
  context['title'] = '-管理员帐号'
  context['main_nav']=main_nav
  
  if modelAdminUser.getLoginUserId(req)==0:
    return HttpResponseRedirect('/login')
  
  url=myurls.ApiGetAdminuserList;
  
  param=[];
  rt=[]
  try:
    rt=json.loads(http.getDateByUrlAndAdminId(url,req,param))
    if rt['response_code'] != 1:
      return tips.error(req,rt['response_message'])
  except HTTPError:
    pass
  context['list']=rt['list']
  return render(req, 'adminuser_list.html', context)

def edit(req):
  '管理员帐号编辑'
  
  
  id=0
  try:
    id=int(str(req.GET.get("id")))
  except ValueError:
    pass
  
  if id==0:
    return tips.error(req,"id不能为0","")
  
  main_nav=[]
  main_nav.append({"url":"/adminuser/","name":"银行限额列表"})
  main_nav.append({"url":"","name":"管理员帐号编辑"})
  context={}
  context['title'] = '-管理员帐号编辑'
  context['main_nav']=main_nav
  context['whether_or_not']=mycategory.getCategoryByName("whether_or_not",req)
  context['available_end_type']=mycategory.getCategoryByName("available_end_type_arr",req)
  
  
  url=myurls.ApiGetAdminuserInfo;
  
  param=[];
  param.append(("id",id))
  rt=[]
  try:
    rt=json.loads(http.getDateByUrlAndAdminId(url,req,param))
    if rt['response_code'] != 1:
      return tips.error(req,rt['response_message'])
  except HTTPError:
    pass
  context['info']=json.loads(rt['info'])
  return render(req, 'adminuser_edit.html', context)


def editpost(req):
  '提交编辑'
  
  rt={}
  rt['response_code']=0
  rt['response_message']="待提交"
  
  param=[]
  
  for key in req.POST:
    v=req.POST.get(key)
    if key.find("ttstamp~")==-1:
      param.append((key,v))
    else:
      if v != "":
        param.append((key.replace("ttstamp~",""),myfc.strToTimestamp(v)))
    
  
  url=myurls.ApiUpdAdminuserInfo;
  try:
    rt=json.loads(http.getDateByUrlAndAdminId(url,req,param))
    if rt['response_code'] != 1:
      return tips.error(req,rt['response_message'])
  except HTTPError:
    pass
  return HttpResponse(json.dumps(rt),content_type="application/json")
  #return tips.success(req,"提交成功","/adminuser/edit/?id="+req.POST.get("id"))


def persion(req):
  '个人中心'
  
  main_nav=[]
  main_nav.append({"url":"","name":"个人中心"})
  context={}
  context['title'] = '-个人中心'
  context['main_nav']=main_nav
  return render(req, 'adminuser_persion.html', context)

def updpwd(req):
  '修改密码'
  
  id=0
  userinfo=[]
  try:
    userinfo=req.session['userinfo']
    id=userinfo['id']
  except KeyError:
    pass
  if id==0:
    return HttpResponseRedirect('/login')
  
  main_nav=[]
  main_nav.append({"url":"/persion","name":"个人中心"})
  main_nav.append({"url":"","name":"修改密码"})
  context={}
  context['title'] = '-修改密码'
  context['main_nav']=main_nav
  context['userinfo']=userinfo
  return render(req, 'adminuser_updpwd.html', context)
  
def updpwdpost(req):
  '校验密码'
    
  url=myurls.ApiUpdAdminuserInfo
  
  param=[];
  
  id=0
  userinfo=[]
  try:
    userinfo=req.session['userinfo']
    id=userinfo['id']
  except KeyError:
    pass
  if id!=0:
    param.append(('id',str(id)))
  
  rt={}
  rt['response_code']=0
  rt['response_message']="待提交"
  
  pwd=req.POST.get("pwd")
  pwd_ck=req.POST.get("pwd_ck")
  if len(pwd)<6 or len(pwd_ck)<6:
    
    rt={}
    rt['response_code']=-1
    rt['response_message']="请输入至少6位数密码"
    return tips.returnAjax(rt)
  if pwd!=pwd_ck:
    rt={}
    rt['response_code']=-1
    rt['response_message']="确认密码不一致"
    return tips.returnAjax(rt)
  
  param.append(('admin_id',id))
  param.append(('admin_pwd',userinfo['password']))
  param.append(('password',http.md5(pwd)))
  
  
  
  
  try:
    rt=json.loads(http.getDateByUrl(url,param))
  except ValueError:
    pass
  return tips.returnAjax(rt)
  #return HttpResponseRedirect('/')
  #return HttpResponse(rt,content_type="application/json")